//
// Created by aaa on 2025/7/10.
//

#include<bits/stdc++.h>

using namespace std;
const int N = 1e4+10;
int a[N],b[N],dp[N][N];
int n;

//最长公共子序列（Longest Common Sequence）LCS
//5
//3 2 1 4 5
//1 2 3 4 5
int main(){
    cin >> n;
    for(int i = 1;i <= n;i++) cin >> a[i];
    for(int i = 1;i <= n;i++) cin >> b[i];

    for(int i = 1;i <= n;i++){
        for(int j = 1;j <= n;j++){
            if(a[i] == b[j]) dp[i][j] = dp[i - 1][j - 1] + 1;
            else dp[i][j] = max(dp[i][j-1],dp[i-1][j]);
        }
    }

    cout << dp[n][n];

    return 0;
}